package hamaster.jpbc.pbc;

import it.unisa.dia.gas.jpbc.Element;

import java.util.Properties;

/**
 * 保存系统公共参数的类
 * @author <a href="mailto:wangyeee@gmail.com">Wang Ye</a>
 */
public class PublicParameter implements Propertiable {
	private final static String G_POSTFIX = "g";
	private final static String G1_POSTFIX = "g1";
	private final static String H_POSTFIX = "h";

	/**
	 * 公共参数G
	 */
	protected Element g;

	/**
	 * 公共参数G1
	 */
	protected Element g1;

	/**
	 * 公共参数H
	 */
	protected Element h;

	public PublicParameter() {
	}

	public PublicParameter(Properties properties, String prefix) {
		setFromProperties(properties, prefix);
	}

	public Element getG() {
		return g;
	}

	public void setG(Element g) {
		this.g = g;
	}

	public Element getG1() {
		return g1;
	}

	public void setG1(Element g1) {
		this.g1 = g1;
	}

	public Element getH() {
		return h;
	}

	public void setH(Element h) {
		this.h = h;
	}

	@Override
	public boolean setFromProperties(Properties properties, String prefix) {
		String gk = G_POSTFIX;
		String g1k = G1_POSTFIX;
		String hk = H_POSTFIX;
		if (prefix != null && prefix.length() > 0) {
			gk = prefix + "." + G_POSTFIX;
			g1k = prefix + "." + G1_POSTFIX;
			hk = prefix + "." + H_POSTFIX;
		}
		String gs = properties.getProperty(gk);
		String g1s = properties.getProperty(g1k);
		String hs = properties.getProperty(hk);
		if (gs == null || g1s == null || hs == null)
			return false;
		PBC pbc = PBC.instance();
		g = pbc.getPairing().getG1().newElement();
		g1 = pbc.getPairing().getG1().newElement();
		h = pbc.getPairing().getG2().newElement();
		g.setFromBytes(PBCDriver.unhex(gs));
		g1.setFromBytes(PBCDriver.unhex(g1s));
		h.setFromBytes(PBCDriver.unhex(hs));
		return true;
	}

	@Override
	public Properties toProperties(String prefix) {
		Properties p = new Properties();
		String gk = G_POSTFIX;
		String g1k = G1_POSTFIX;
		String hk = H_POSTFIX;
		if (prefix != null && prefix.length() > 0) {
			gk = prefix + "." + G_POSTFIX;
			g1k = prefix + "." + G1_POSTFIX;
			hk = prefix + "." + H_POSTFIX;
		}
		p.setProperty(gk, PBCDriver.hex(g.toBytes()));
		p.setProperty(g1k, PBCDriver.hex(g1.toBytes()));
		p.setProperty(hk, PBCDriver.hex(h.toBytes()));
		return p;
	}

	@Override
	public String toString() {
		return "[PublicParameter]\ng=" + g + "\ng1=" + g1 + "\nh=" + h + "\n";
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PublicParameter other = (PublicParameter) obj;
		if (g == null) {
			if (other.g != null)
				return false;
		} else if (!g.equals(other.g))
			return false;
		if (g1 == null) {
			if (other.g1 != null)
				return false;
		} else if (!g1.equals(other.g1))
			return false;
		if (h == null) {
			if (other.h != null)
				return false;
		} else if (!h.equals(other.h))
			return false;
		return true;
	}

}
